<template>
  <div class="active">
    <h2>供应商评审总分与排名</h2>
    <el-table :data="supplierReviews" border style="width: 100%">
      <el-table-column
        prop="key"
        label="序号"
        width="81"
        align="center"
      ></el-table-column>
      <el-table-column
        prop="supplierCode"
        label="供应商编码"
        width="150"
        align="center"
      ></el-table-column>
      <el-table-column
        prop="supplierName"
        label="报名供应商"
        width="207"
        align="center"
      ></el-table-column>
      <el-table-column
        prop="priceScore"
        label="价格分"
        width="80"
        align="center"
        :formatter="formatScore"
      ></el-table-column>

      <el-table-column
        prop="businessScore"
        label="商务分"
        width="80"
        align="center"
        :formatter="formatScore"
      ></el-table-column>
      <el-table-column label="技术分" align="center">
        <template
          v-for="(item, index) in supplierReviews[0]?.technicalScores || []"
          :key="index"
        >
          <el-table-column
            :prop="`technicalScores[${index}]`"
            :label="`评委${index + 1}`"
            align="center"
          >
            <template #default="scope">
              <div>{{ scope.row.technicalScores[index] }}</div>
            </template>
          </el-table-column>
        </template>
        <el-table-column
          prop="averageTechnicalScore"
          label="平均分"
          width="70"
          align="center"
        ></el-table-column>
      </el-table-column>
      <el-table-column
        prop="totalScore"
        label="综合得分"
        width="100"
        align="center"
        fixed="right"
      ></el-table-column>
      <el-table-column
        prop="rank"
        label="排名"
        width="70"
        align="center"
        fixed="right"
      ></el-table-column>
    </el-table>
    <h3>项目评审结果</h3>
    <!-- 项目评审结果 -->
    <el-table
      :data="projectReviews"
      border
      style="width: 100%; margin-top: 20px"
    >
      <el-table-column
        prop="key"
        label="序号"
        width="81"
        align="center"
      ></el-table-column>
      <el-table-column
        prop="applyTime"
        label="报名时间"
        width="150"
        align="center"
      ></el-table-column>
      <el-table-column
        prop="supplierCode"
        label="供应商编码"
        width="200"
        align="center"
      ></el-table-column>
      <el-table-column
        prop="name"
        label="报名供应商"
        width="300"
        align="center"
      ></el-table-column>
      <el-table-column
        prop="status"
        label="资质审查"
        width="100"
        align="center"
      ></el-table-column>
      <el-table-column
        prop="score"
        label="综合得分"
        align="center"
      ></el-table-column>
      <el-table-column
        prop="reviewResult"
        label="评审结果"
        width="100"
        align="center"
      ></el-table-column>
    </el-table>
    <div
      style="display: flex; align-items: center; justify-content: space-between"
    >
      <h2>中选供应商结果确认</h2>
      <el-button type="primary" @click="getInfo(true)">刷新</el-button>
    </div>
    <el-form :model="SelectedForm" label-width="250px">
      <div v-if="!Stop">
        <el-form-item label="第一候选人：">
          <el-input
            style="width: 500px"
            v-model="SelectedForm.outbidOneId"
            disabled
          ></el-input>
        </el-form-item>
        <el-form-item label="中选理由：" >
          <el-input
            style="width: 500px"
            v-model="SelectedForm.remarkOne"
            disabled
          ></el-input>
        </el-form-item>
        <el-form-item label="第二候选人：" >
          <el-input
            style="width: 500px"
            v-model="SelectedForm.outbidTwoId"
            disabled
          ></el-input>
        </el-form-item>
        <el-form-item label="中选理由：" >
          <el-input
            style="width: 500px"
            v-model="SelectedForm.remarkTwo"
            disabled
          ></el-input>
        </el-form-item>
        <el-form-item label="第三候选人：" >
          <el-input
            style="width: 500px"
            v-model="SelectedForm.outbidThreeId"
            disabled
          ></el-input>
        </el-form-item>
        <el-form-item label="中选理由：" >
          <el-input
            style="width: 500px"
            v-model="SelectedForm.remarkThree"
            disabled
          ></el-input>
        </el-form-item>
      </div>
      <div v-else>
        <el-form-item label="强制终止评审原因：">
          <el-input
            style="width: 500px"
            v-model="SelectedForm.breakRemark"
          ></el-input>
        </el-form-item>
      </div>
      <el-form-item label="主持人签名确认结果时间：">
        <el-input
          style="width: 500px"
          v-model="SelectedForm.restartCheck.restartCheckTime"
          disabled
        ></el-input>
      </el-form-item>
      <el-form-item label="主持人评审结果确认签名：">
        <div class="tablet">
          <el-image
            v-if="SelectedForm.restartCheck.restartCheck"
            :src="baseURL + SelectedForm.restartCheck.restartCheck"
          >
            <template #error>
              <div class="image-slot"></div>
            </template>
          </el-image>
        </div>
      </el-form-item>

      <el-form-item
        :label="`评委${index + 1}(${item.name})评审结果确认签名：`"
        v-for="(item, index) in SelectedForm.Signlist"
        :key="index"
      >
        <div class="tablet">
          <el-image v-if="item.restartCheck" :src="baseURL + item.restartCheck">
            <template #error>
              <div class="image-slot"></div>
            </template>
          </el-image>
        </div>
      </el-form-item>
      <el-form-item label="监督人评审结果确认签名：">
        <div class="tablet">
          <el-image
            v-if="SelectedForm.supervisorSignature.restartCheck"
            :src="baseURL + SelectedForm.supervisorSignature.restartCheck"
          >
            <template #error>
              <div class="image-slot"></div>
            </template>
          </el-image>
        </div>
      </el-form-item>
    </el-form>
  </div>
</template>
<script setup>
import { ref } from "vue";
import { useRoute } from "vue-router";
import {
  totalScore,
  projectInfoAndSupplierAndJudge,
  queryTaskId,
} from "@/api/examine.js";
import { ElMessage } from "element-plus";

const emit = defineEmits(["next-step"]);
const route = useRoute();
const id = computed(() => route.query.id);
const judgeId = ref("");
// 定义数据
const SelectedForm = ref({
  outbidOneId: "",
  remarkOne: "",
  outbidTwoId: "",
  remarkTwo: "",
  outbidThreeId: "",
  remarkThree: "",
  restartCheckTime: "",
  restartCheck: "",
  Signlist: [],
  supervisorSignature: "",
});
const baseURL = import.meta.env.VITE_APP_BASE_API;
const Stop = ref(false);
const supplierReviews = ref([]);
const projectReviews = ref([]);
const candidates = ref([]);
const getInfo = (boolean) => {
  SelectedForm.value = {
    outbidOneId: "",
    remarkOne: "",
    outbidTwoId: "",
    remarkTwo: "",
    outbidThreeId: "",
    remarkThree: "",
    restartCheckTime: "",
    restartCheck: "",
    Signlist: [],
    supervisorSignature: "",
  };
  totalScore({ projectId: id.value }).then((res) => {
    if (res.code == "200") {
      supplierReviews.value = [];

      for (let i = 0; i < res.data.length; i++) {
        let technicalScores = []; // 技术分数组
        let averageTechnicalScore = null; // 技术平均分
        const scoreMap = {}; // 用于跟踪技术分的累积

        for (let j = 0; j < res.data[i].scoreList.length; j++) {
          const scoreItem = res.data[i].scoreList[j];

          // 根据 type 添加到相应的属性中或进行累加
          if (scoreItem.type === "score_skill") {
            if (!scoreMap[scoreItem.markId]) {
              scoreMap[scoreItem.markId] = scoreItem.score;
              technicalScores.push(scoreItem.score);
            } else {
              // 如果已经存在相同的 markId，则累加分数
              scoreMap[scoreItem.markId] += scoreItem.score;
              // 更新 technicalScores 中对应的值（假设顺序一致）
              const index = technicalScores.findIndex(
                (score) =>
                  score === scoreMap[scoreItem.markId] - scoreItem.score
              );
              if (index !== -1) {
                technicalScores[index] = scoreMap[scoreItem.markId];
              }
            }
          } else if (scoreItem.type === "score_average") {
            averageTechnicalScore = scoreItem.score;
          }
        }

        // 构建 supplierReviews.value 数组
        supplierReviews.value.push({
          key: i + 1,
          supplierCode: res.data[i].supplierCode,
          supplierName: res.data[i].supplierName,
          priceScore: null, // 初始化为 null
          businessScore: null, // 初始化为 null
          technicalScores: technicalScores,
          averageTechnicalScore: averageTechnicalScore,
          totalScore: res.data[i].totalScore,
          rank: i + 1,
        });

        // 查找价格分和商务分
        for (let j = 0; j < res.data[i].scoreList.length; j++) {
          const scoreItem = res.data[i].scoreList[j];
          if (scoreItem.type === "score_price") {
            supplierReviews.value[supplierReviews.value.length - 1].priceScore =
              scoreItem.score;
          } else if (scoreItem.type === "score_business") {
            supplierReviews.value[
              supplierReviews.value.length - 1
            ].businessScore = scoreItem.score;
          }
        }

        // 构建 candidates.value 数组
      }
      console.log(supplierReviews.value);
    }
  });
  try {
    projectInfoAndSupplierAndJudge({ id: id.value }).then((res) => {
      console.log(res);
      if (res.code == "200") {
        candidates.value = [];
        projectReviews.value = [];
        SelectedForm.value.outbidOneId = res.data.outbidOneId;
        SelectedForm.value.outbidTwoId = res.data.outbidTwoId;
        SelectedForm.value.outbidThreeId = res.data.outbidThreeId;
        SelectedForm.value.remarkOne = res.data.remarkOne;
        SelectedForm.value.remarkTwo = res.data.remarkTwo;
        SelectedForm.value.remarkThree = res.data.remarkThree;
        if (res.data.breakRemark) {
          Stop.value = true;

          SelectedForm.value.breakRemark = res.data.breakRemark;
        }

        for (let i = 0; i < res.data.judgeList.length; i++) {
          switch (res.data.judgeList[i].type) {
            case "judge_jury":
              SelectedForm.value.Signlist.push({
                id: res.data.judgeList[i].id,
                restartCheck: res.data.judgeList[i].endCheck,
                name: res.data.judgeList[i].name,
              });
              break;
            case "judge_custodian":
              SelectedForm.value.supervisorSignature = {
                id: res.data.judgeList[i].id,
                restartCheck: res.data.judgeList[i].endCheck,
              };
              break;
            case "judge_compere":
              console.log(963, res.data.judgeList[i]);

              judgeId.value = res.data.judgeList[i].id;
              SelectedForm.value.restartCheck = {
                id: res.data.judgeList[i].id,
                restartCheck: res.data.judgeList[i].endCheck,
                restartCheckTime: res.data.judgeList[i].endCheckTime,
              };
              break;
          }
        }

        for (let i = 0; i < res.data.supplierList.length; i++) {
          candidates.value.push({
            id: res.data.supplierList[i].supplierId,
            name: res.data.supplierList[i].name,
          });
          const formattedDateTime = res.data.supplierList[i].applyTime.slice(
            0,
            16
          );
          let reviewResult = "";
          if (res.data.outbidOneId == res.data.supplierList[i].supplierId) {
            reviewResult = "第一候选人";
          } else if (
            res.data.outbidTwoId == res.data.supplierList[i].supplierId
          ) {
            reviewResult = "第二候选人";
          } else if (
            res.data.outbidThreeId == res.data.supplierList[i].supplierId
          ) {
            reviewResult = "第三候选人";
          } else {
            reviewResult = "待定";
          }
          projectReviews.value.push({
            key: i + 1,
            applyTime: formattedDateTime,
            supplierCode: res.data.supplierList[i].supplierCode,
            name: res.data.supplierList[i].name,
            status:
              res.data.supplierList[i].status == "apply_supplier_pass"
                ? "符合"
                : "异常",
            score: res.data.supplierList[i].score,
            reviewResult: reviewResult,
          });
        }
        if (Stop.value == true) {
          candidates.value.push({
            id: "",
            name: "无",
          });
        }
        console.log(projectReviews.value);
        if (boolean) {
          ElMessage({
            message: "刷新成功",
            type: "success",
          });
        }
      }
    });
  } catch (error) {}
};
const formatScore = (row, column, cellValue) => {
  return cellValue < 0 ? 0 : cellValue;
};
const Previous = ref("");
onMounted(() => {
  getInfo();
  queryTaskId({ projectId: id.value }).then((res) => {
    Previous.value = res.data;
  });
});
</script>

<style scoped lang="scss">
@use "./judge";
</style>
